package com.ruoyi.web.controller.erp.componentstatus;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.erp.componentstatus.domain.ComponentStatus;
import com.ruoyi.erp.componentstatus.service.ComponentStatusService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("componentStatus")
public class ComponentStatusController extends BaseController {
    @Autowired
    private ComponentStatusService componentStatusService;


    @GetMapping("/list")
    public TableDataInfo list(ComponentStatus componentStatus){
        List<ComponentStatus> list = componentStatusService.findAll(componentStatus);
        //list.stream().forEach(le -> le.setRowversion(DateUtils.parseDate(le.getRowversion())));
        return  getDataTable(list);
    }


    @PostMapping
    public AjaxResult add(@RequestBody ComponentStatus componentStatus){
        componentStatus.setRowkey(UUID.randomUUID().toString());
        componentStatus.setRowversion(DateUtils.getTime());
        componentStatusService.add(componentStatus);
        return toAjax(1);
    }

    @PutMapping
    public AjaxResult update(@RequestBody ComponentStatus componentStatus){
        componentStatusService.update(componentStatus);
        return toAjax(1);
    }

    @PutMapping("/changeStatus")
    public AjaxResult updateStatus(@RequestBody Map<String,String> map){
        ComponentStatus componentStatus = new ComponentStatus();
        componentStatus.setRowkey(map.get("rowkey"));
        componentStatus.setDefaultStatus(map.get("defaultStatus"));
        componentStatusService.update(componentStatus);
        return toAjax(1);
    }

    @DeleteMapping("/{rowkey}")
    public AjaxResult delete(@PathVariable String rowkey[]){

        componentStatusService.delete(rowkey);
        return toAjax(1);
    }


    @GetMapping("/{rowkey}")
    public AjaxResult getInfoById(@PathVariable String[] rowkey ){
        return AjaxResult.success(componentStatusService.findAllById(rowkey));
    }

    @PostMapping("/export")
    public AjaxResult export(@RequestBody String[] ids){

        ExcelUtil<ComponentStatus> util = new ExcelUtil<ComponentStatus>(ComponentStatus.class);
        AjaxResult ajaxResult = util.exportExcel(componentStatusService.findAllById(ids), "报废原因");
        return ajaxResult;
    }






}
